<template>
  <div class="add-menu">
    <el-card header="添加菜单">
      <el-form :model="formMessage" ref="form" label-width="80px">
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="formMessage.name" type="text" clearable placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="菜单路径" prop="href">
          <el-input v-model="formMessage.href" type="text" clearable placeholder="请输入菜单路径" />
        </el-form-item>
        <el-form-item label="上级菜单" prop="parentId">
          <el-select v-model="formMessage.parentId" clearable placeholder="请选择上级菜单">
            <el-option v-for="item in menuList" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="formMessage.description" type="text" clearable placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="前端图标" prop="icon">
          <el-input v-model="formMessage.icon" type="text" clearable placeholder="请输入前端图标" />
        </el-form-item>
        <el-form-item label="是否显示" prop="shown">
          <el-radio-group v-model="formMessage.shown">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input-number v-model="formMessage.orderNum" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="submitting" @click="handleSubmit">提交</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import { getAllMenus, saveOrUpdateMenu, getMenuDetail } from '@/services/boss'
import { Form, Message } from 'element-ui'
import { Vue, Component } from 'vue-property-decorator'

@Component
export default class AddMenu extends Vue {
  private formMessage: any = {
    name: '',
    parentId: 1,
    href: '',
    icon: '',
    orderNum: 0,
    description: '',
    shown: true
  }

  private formRules: any = {
    name: [
      { required: true, message: '请输入菜单名称', trigger: 'blur' }
    ],
    path: [
      { required: true, message: '请输入菜单路径', trigger: 'blur' }
    ]
  }

  private menuList: Array<any> = []
  private submitting = false
  private isEdit = false

  async mounted () {
    const { data } = await getAllMenus()
    this.menuList = data || []
    if (this.$route.params.id) {
      this.isEdit = true
      const response = await getMenuDetail(parseInt(this.$route.params.id))
      for (const key in this.formMessage) {
        this.formMessage[key] = response.data[key]
      }
    }
  }

  async handleSubmit () {
    const res = await (this.$refs.form as Form).validate()
    if (res) {
      try {
        const params = Object.assign({}, this.formMessage)
        if (this.isEdit) {
          params.id = this.$route.params.id
        }
        this.submitting = true
        await saveOrUpdateMenu(params)
        Message.success(`${this.isEdit ? '编辑' : '添加'}菜单成功`)
        this.$router.back()
      } catch (e: any) {
        Message.error(e.message)
      } finally {
        this.submitting = false
      }
    }
  }

  handleReset () {
    (this.$refs.form as Form).resetFields()
  }
}
</script>

<style lang="scss" scoped>

</style>
